<?php
/**
 * Description: 获取全局配置
 * User: devforma
 * Date: 15/1/7
 * Time: 15:46
 */

require 'includes/Note.class.php';

class GetGlobalSettings implements iController {
    const ENTITY_ID = 'entity:spark:dailyplan:GetGlobalSettings';

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        $result = [];

        //etag版本控制
        if (ENABLE_ETAG) {
            $result['etag'] = getEtag(self::ENTITY_ID);

            if (!empty($queryArgs['etag']) && $result['etag'] === $queryArgs['etag']) {
                return genOutput(302, $result);
            }
        }

        $result['service_qq'] = getOption('service_qq');

        //ios的review版本号控制，当前版本号小于设置的review版本号则为非review状态，否则为review状态
        if (!empty($queryArgs['terminal_type']) && strpos($queryArgs['terminal_type'], 'iOS') !== false && !empty($queryArgs['app_version'])) {
            $cmp = version_cmp($queryArgs['app_version'], getOption('dp_review_version'));
            if ($cmp == -1) {
                $result['is_review'] = false;
            } else {
                $result['is_review'] = true;
            }
        }

        //分享地址
        $result['app_ios_url'] = 'http://t.cn/R2a6wtv';
        $result['app_android_url'] = 'http://t.cn/RyX5ro9';
        $result['app_share_url'] = 'http://m.yaoguo.cn';
        $result['question_share_url'] = YG_MOBILE_URL . '/appShare/index.html#/appShare/dailyplan?question_id=';

        //课程分享地址
        $result['course_share_url'] = YG_MOBILE_URL . '/terminalType/shareCourse.html?course_id=';

        //获取新版本更新信息
        $queryArgs['app_version'] = !empty($queryArgs['app_version']) ? $queryArgs['app_version'] : '0.0';
        $newVersion = $this->getNewVersion($queryArgs['terminal_type'], $queryArgs['app_version']);
        $result['new_version'] = $newVersion;


        //评价文字标签
        $tags = array();
        $starTags = getOption('star_tags');
        if (!empty($starTags) && is_array($starTags)) {
            foreach ($starTags as $index => $tag) {
                $tags[] = array(
                    'star' => intval($index),
                    'tags' => explode(',', $tag)
                );
            }
        }
        $result['rate_tags'] = $tags;

        //最新通知
        $result['latest_notify'] = $this->getLatestNotify();

        if(!empty($queryArgs['user_id'])) {
            $note = new Note();
            $note->updateUserNotes($queryArgs['user_id']);
        }

        //微信广告数据追踪
        if (!empty($queryArgs['idfa'])){
            youqianIdfa('dailyplan', $queryArgs['idfa']);
            weixinIdfa('dailyplan', $queryArgs['idfa']);
            pandaPlayIdfa('dailyplan',$queryArgs['idfa']);
        }

        return genOutput(1, $result);
    }

    /**
     * 版本更新配置
     *
     * @param string $terminalType 设备类型
     * @param string $appVersion app版本号
     * @return array|stdClass
     */
    private function getNewVersion($terminalType, $appVersion) {
        $upgradeLogs = getAppUpgradeLog('dailyplan', $terminalType === 'android_phone' ? 'Android' : 'iOS', $appVersion);

        if (!empty($upgradeLogs))
            return array(
                'app_version'  => $upgradeLogs->app_version,
                'size'         => $upgradeLogs->package_size,
                'notice_text'  => $upgradeLogs->content,
                'force_update' => (bool)$upgradeLogs->force_upgrade,
                'target_url'   => !empty($upgradeLogs->package_url) ? $upgradeLogs->package_url : ''
            );

        return new stdClass();
    }

    /**
     * 获取最新的通知id，用于app端小红点的展示
     */
    private function getLatestNotify() {
        global $cmsdb;

        $cacheKey = 'func:getLatestNotify:dailyplan';
        if (($value = cacheGet($cacheKey)) == null) {
            $sql = 'SELECT `id` FROM `qb_notify` WHERE `app_type`="dailyplan" ORDER BY `id` DESC LIMIT 1';
            $value = $cmsdb->getVar($sql);

            cacheAdd($cacheKey, $value, 10*60);
        }

        return intval($value);
    }
}